package cn.engage.org.service.impl;

import cn.engage.basic.config.BussinessException;
import cn.engage.basic.query.BaseQuery;
import cn.engage.basic.service.impl.BaseServiceImpl;
import cn.engage.basic.util.BaiduAuditUtils;
import cn.engage.basic.util.MailUtil;
import cn.engage.basic.util.PageList;
import cn.engage.basic.vo.ShopEchartsVo;
import cn.engage.org.domain.Employee;
import cn.engage.org.domain.Shop;
import cn.engage.org.domain.ShopAuditLog;
import cn.engage.org.mapper.EmployeeMapper;
import cn.engage.org.mapper.ShopAuditLogMapper;
import cn.engage.org.mapper.ShopMapper;
import cn.engage.org.service.ShopService;
import com.baomidou.mybatisplus.extension.api.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private JavaMailSender sender;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;



    /*店铺入驻*/
    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        Employee employee = getEmployee(shop, admin);

        shopMapper.add(shop);
        if (null == employee){
            employee = admin;
            employeeMapper.add(employee);
        }
        employee.setShopId(shop.getId());
        employeeMapper.update(employee);
        Shop uptShop = new Shop();
        uptShop.setId(shop.getId());
        uptShop.setAdminId(employee.getId());
        shopMapper.update(uptShop);
    }
    /* 审核通过*/
    @Override
    public void auditPass(ShopAuditLog shopAuditLog) {
        Shop shop = new Shop();
        shop.setId(shopAuditLog.getShopId());
        // 修改状态为未激活
        shop.setState(1);
        shopMapper.update(shop);
        // 还没做登陆注册 审核人id写死
        shopAuditLog.setAuditId(111L);
        shopAuditLogMapper.add(shopAuditLog);
        // 发送激活邮件
        MailUtil.petHomeMail(sender,shopAuditLog,shop);

    }
    /* 审核驳回*/
    @Override
    public void auditReject(ShopAuditLog shopAuditLog) {

        Shop shop = new Shop();
        shop.setId(shopAuditLog.getShopId());
        // 修改状态为驳回
        shop.setState(-1);
        shopAuditLog.setAuditId(111L);
        shopAuditLogMapper.add(shopAuditLog);
        shopMapper.update(shop);
        // 发送激活邮件
        MailUtil.petHomeMail(sender,shopAuditLog,shop);
    }
    /* 邮箱审核驳回修改店铺信息*/
    @Override
    public void faild(Shop shop) {
        extracted(shop);
        shop.setState(0);
        shopMapper.update(shop);
    }

    // 新增修改店铺的判断条件
    private void extracted(Shop shop) {
        // 店铺名字不能为空
        if (StringUtils.isBlank(shop.getName())){
            throw new BussinessException("店铺名字不能为空");
        }
        // AI验证
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BussinessException("店铺名称太涩情");
        }
        ;
        if (!BaiduAuditUtils.ImgCensor(shop.getLogo())){
            throw new BussinessException("图片太涩情");
        }
    }

    // 前端报表
    @Override
    public Map<String, Object> echarts() {
        Map<String,Object> result = new HashMap<>();
        List<ShopEchartsVo> shopEchartsVos = shopMapper.echarts();
        ArrayList<String> x = new ArrayList<>();
        ArrayList<Integer> y = new ArrayList<>();
        for (ShopEchartsVo shopEchartsVo : shopEchartsVos) {
            if (shopEchartsVo.getState() == 1){
                String a = "已审核";
                x.add(a);
                y.add(shopEchartsVo.getCount());
            } else if (shopEchartsVo.getState() == 0){
                String a = "待审核";
                x.add(a);
                y.add(shopEchartsVo.getCount());
            }else if (shopEchartsVo.getState() == 2){
                String a = "已激活";
                x.add(a);
                y.add(shopEchartsVo.getCount());
            }else{
                String a = "驳回";
                x.add(a);
                y.add(shopEchartsVo.getCount());
            }
        }
        result.put("x",x);
        result.put("y",y);

        return result;
    }
    // 驳回邮件回显数据
    @Override
    public Shop findByIdOne(Long id) {
        return shopMapper.findByIdOne(id);
    }

    // 通过qq邮件激活
    @Override
    public void active(Long id) {
        Shop shop = new Shop();
        shop.setId(id);
        shop.setState(2);
        shopMapper.update(shop);
    }
    /* 批量添加 */
    @Override
    public void batchAdd(List<Shop> shops) {
        for (Shop shop : shops) {
            shop.setRegisterTime(new Date());
        }
        shopMapper.batchAdd(shops);
    }

    private Employee getEmployee(Shop shop, Employee admin) {
        // 店铺名字不能为空
        if (StringUtils.isBlank(shop.getName())){
            throw new BussinessException("店铺名字不能为空");
        }
        // 验证密码
        if (StringUtils.isBlank(admin.getPassword())){
            throw new BussinessException("请输入密码");
        }else {
            if (!admin.getPassword().equals(admin.getComfirmPassword())){
                throw new BussinessException("两次输入的密码不一致");
            }
        }
        // AI验证
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BussinessException("店铺名称太涩情");
        };
        if (!BaiduAuditUtils.ImgCensor(shop.getLogo())){
            throw new BussinessException("图片太涩情");
        }

        // 验证店铺是否已经存在
        Shop shopByName = shopMapper.findByName(shop.getName());
        if (null != shopByName){
            throw new BussinessException("店铺已经存在");
        }
        // 验证当前注册人是否已经管理了店铺
        Employee employee = employeeMapper.findByName(admin.getUsername());
        if (employee != null && employee.getShopId() != null){
            if (employee.getShopId().equals(shop.getId())){
                throw new BussinessException("当前用户已管理了店铺");
            }
        }
        return employee;
    }

    @Override
    public void update(Shop shop) {
        extracted(shop);
        super.update(shop);

    }

    @Override
    public void add(Shop shop) {
        extracted(shop);
        super.add(shop);
    }

    @Override
    public PageList<Shop> queryByPage(BaseQuery query) {
        PageList<Shop> shopPageList = super.queryByPage(query);
        List<Shop> rows = shopPageList.getRows();
         Map<Long,List<ShopAuditLog>> map = new HashMap<>();
        for (Shop row : rows) {
            List<ShopAuditLog> shopAuditLogs = shopAuditLogMapper.findByShopId(row.getId());
            map.put(row.getId(),shopAuditLogs);
        }
        rows.forEach(row->{
            row.setShopAuditLogs(map.get(row.getId()));
        });
        return shopPageList;
    }
}
